package com.lsp.lib_uikit.commonrecycler.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.lsp.lib_uikit.commonrecycler.rvitem.ListItem;

/**
 * 单itemAdapter
 *
 * @author lsp
 * @created 16/1/21 15:23
 */
public class CommonRvAdapter<D> extends BasicRvAdapter<D> {
  @LayoutRes private int layoutId;//itemId
  private Context mContext;
  private Handler handler;

  public CommonRvAdapter(Context mContext, @LayoutRes int layoutId) {
    super(mContext);
    this.mContext = mContext;
    this.layoutId = layoutId;
  }

  public CommonRvAdapter(Context mContext, @LayoutRes int layoutId, Handler handler) {
    super(mContext);
    this.mContext = mContext;
    this.layoutId = layoutId;
    this.handler = handler;
  }

  @Override public int getDefItemViewType(int position) {
    return 0;
  }

  @Override public RecyclerView.ViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
    ListItem<D> view = (ListItem<D>) LayoutInflater.from(mContext).inflate(layoutId, parent, false);
    //CommonHolder holder = new CommonHolder(view);
    //return holder;
    return new RecyclerView.ViewHolder(view) {
    };
  }

  @Override protected void onDefBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    if (holder == null) {
      return;
    }
    final ListItem<D> view = (ListItem<D>) holder.itemView;
    view.setFirst(position == 0);
    view.setPosition(position);
    view.setLast(position == getItemCount() - 1);
    if (handler != null) {
      view.setHandler(handler);
    }
    bindDataToView(view, getAllList().get(position));
  }

  protected void bindDataToView(ListItem<D> view, D data) {
    view.attachData(data);
  }
}
